package com.testtencentpull.demo;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.tencent.android.tpush.XGPushBaseReceiver;
import com.tencent.android.tpush.XGPushClickedResult;
import com.tencent.android.tpush.XGPushRegisterResult;
import com.tencent.android.tpush.XGPushShowedResult;
import com.tencent.android.tpush.XGPushTextMessage;


public class CustomPushReceiver extends XGPushBaseReceiver {
	public static final String LogTag = "TPushReceiver";

	private void show(Context context, String text) {
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}
	@Override
	public void onRegisterResult(Context context, int errorCode,
			XGPushRegisterResult registerMessage) {
		if (context == null || registerMessage == null) {
			return;
		}
		String text = null;
		if (errorCode == XGPushBaseReceiver.SUCCESS) {
			text = registerMessage +"message";
			String token = registerMessage.getToken();
		} else {
			text = registerMessage + "-------" + errorCode;
		}
		Log.d(LogTag, text);
		show(context, text);
	}


	@Override
	public void onUnregisterResult(Context context, int errorCode) {
		if (context == null) {
			return;
		}
		String text = null;
		if (errorCode == XGPushBaseReceiver.SUCCESS) {
			text = "xxxxxxxxxxx";
		} else {
			text = "yyyyyyyyyyyyy" + errorCode;
		}
		Log.d(LogTag, text);
		show(context, text);
	}

	@Override
	public void onSetTagResult(Context context, int errorCode, String tagName) {
		if (context == null) {
			return;
		}
		String text = null;
		if (errorCode == XGPushBaseReceiver.SUCCESS) {
			text = "\"" + tagName + "------------";
		} else {
			text = "\"" + tagName + "///////////" + errorCode;
		}
		Log.d(LogTag, text);
		show(context, text);
	}

	@Override
	public void onDeleteTagResult(Context context, int errorCode, String tagName) {
		if (context == null) {
			return;
		}
		String text = null;
		if (errorCode == XGPushBaseReceiver.SUCCESS) {
			text = "\"" + tagName + "4444444444";
		} else {
			text = "\"" + tagName + "55555555555555555" + errorCode;
		}
		Log.d(LogTag, text);
		show(context, text);
	}

	@Override
	public void onTextMessage(Context context, XGPushTextMessage message) {
		if (context == null || message == null) {
			return;
		}
		String text = "..........." + message.toString();
		String customContent = message.getCustomContent();
		if (customContent != null && customContent.length() != 0) {
			try {
				JSONObject obj = new JSONObject(customContent);
				if (!obj.isNull("key")) {
					String value = obj.getString("key");
					Log.d(LogTag, "get custom value:" + value);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		Log.d(LogTag, text);
		show(context, text);
	}

	@Override
	public void onNotifactionClickedResult(Context context,
			XGPushClickedResult message) {
		if (context == null || message == null) {
			return;
		}
		String text = "ͨbbbbbb:" + message;
		String customContent = message.getCustomContent();
		if (customContent != null && customContent.length() != 0) {
			try {
				JSONObject obj = new JSONObject(customContent);
				if (!obj.isNull("key")) {
					String value = obj.getString("key");
					Log.d(LogTag, "get custom value:" + value);
				}
				// ...
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		Log.d(LogTag, text);
	}

	@Override
	public void onNotifactionShowedResult(Context context,
			XGPushShowedResult notifiShowedRlt) {
		if (context == null || notifiShowedRlt == null) {
			return;
		}
		String text = "ͨitle:" + notifiShowedRlt.getTitle()
				+ ",content:" + notifiShowedRlt.getContent()
				+ ",custom_content:" + notifiShowedRlt.getCustomContent();
		Log.d(LogTag, text);
		show(context, text);
	}
}
